﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FmHandOP : Form
    {

        MySocket iSocket;
        KapjinEqProtocol kep;

        string ip1 = System.Configuration.ConfigurationManager.AppSettings["WCSAddress"];
        int port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort"]);
        int port1 = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort1"]);
        int port2 = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WCSPort2"]);
        static SocketTest st3 = new SocketTest();
        public FmHandOP()
        {
            InitializeComponent();
        }

        //校验并发送读指令
        public string ReadRightValue3(string eqadr, int eqno, string expectvalue)
        {
            int ngcount = 1;
            string recvalue = "";

            if (expectvalue != "notnull")
            {
                while (ngcount <= 3 && recvalue != expectvalue)
                {
                    if (st3.Connected() == false)
                    {
                        //richtbxmsg.AppendText("网络连接断开");
                        richtbxmsg.AppendText("网络连接断开\r\n");
                        //WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st3.Connected() == false)
                        {
                            ReConnect3();
                        }

                    }
                    else if (st3.Connected() == true)
                    {
                        bool rdflag = st3.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            //WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }

                        Thread.Sleep(100);
                        recvalue = st3.receivedata(eqno, "r");
                        ngcount++;
                    }


                }
            }
            else
            {
                while (ngcount <= 3 && recvalue == "")
                {
                    if (st3.Connected() == false)
                    {
                        //richtbxmsg.AppendText("网络连接断开");
                        richtbxmsg.AppendText("网络连接断开\r\n");
                        //WriteLogTxt(DateTime.Now + " " + "网络连接断开");
                        while (st3.Connected() == false)
                        {
                            ReConnect3();
                        }
                    }
                    else if (st3.Connected())
                    {
                        bool rdflag = st3.sendRddata(eqadr, eqno);
                        if (!rdflag)
                        {
                            //WriteLogTxt(DateTime.Now + "  读取" + eqadr + " 寄存器失败");
                        }
                        Thread.Sleep(200);
                        recvalue = st3.receivedata(eqno, "r");
                        ngcount++;
                    }

                }
            }

            Thread.Sleep(200);
            return recvalue;

        }

        //连接网络
        private void ReConnect3()
        {
            if (!st3.Connected())
            {
                richtbxmsg.AppendText("与堆垛机网络连接中断\r\n");
                try
                {
                    st3.CreateClient(ip1, port2);
                }
                catch (Exception)
                {

                }

            }
            else
            {

                richtbxmsg.AppendText("与堆垛机网络连接恢复\r\n");
                return;
            }
        }



        //校验并发送写指令
        public void WriteRightValue3(int eqadr, int eqno, int value)
        {
            int ngcount = 1;
            string recvalue = "";
            bool sendflag = false;

            while (sendflag != true)
            {
                if (st3.Connected() == false)
                {
                    richtbxmsg.AppendText("网络连接断开\r\n");

                    //WriteLogTxt(DateTime.Now + " 网络连接断开");
                    while (st3.Connected() == false)
                    {
                        ReConnect3();
                    }

                }
                else if (st3.Connected())
                {
                    sendflag = st3.sendWrdata(eqadr, eqno, value);
                    if (!sendflag)
                    {
                        //WriteLogTxt(DateTime.Now + "  " + eqadr + "寄存器写" + value + "失败");

                    }
                    Thread.Sleep(100);
                    recvalue = st3.receivedata(1, "w");
                    ngcount++;

                }

            }
            Thread.Sleep(200);

        }


    
        private void button1_Click(object sender, EventArgs e)
        {
            bgmove.RunWorkerAsync();

        }

        private void FmHandOP_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            st3.CreateClient(ip1, port2);
            listboxstageloc.Items.Add("1-1-1");
            listboxstageloc.Items.Add("1-1-2");
            listboxstageloc.Items.Add("1-1-3");
            listboxstageloc.Items.Add("1-2-1");
            listboxstageloc.Items.Add("1-2-2");
            listboxstageloc.Items.Add("1-2-3");
            listboxstageloc.Items.Add("1-3-1");
            listboxstageloc.Items.Add("1-3-2");
            listboxstageloc.Items.Add("1-3-3");
            listboxstageloc.Items.Add("1-4-1");
            listboxstageloc.Items.Add("1-4-2");
            listboxstageloc.Items.Add("1-4-3");
            listboxstageloc.Items.Add("1-5-1");
            listboxstageloc.Items.Add("1-5-2");
            listboxstageloc.Items.Add("1-5-3");
            listboxstageloc.Items.Add("1-6-1");
            listboxstageloc.Items.Add("1-6-2");
            listboxstageloc.Items.Add("1-6-3");
            listboxstageloc.Items.Add("1-9-1");
            listboxstageloc.Items.Add("1-9-2");
            listboxstageloc.Items.Add("1-9-3");
            //iSocket = new MySocket();

            
        }

        private void btin_Click(object sender, EventArgs e)
        {
            bgin.RunWorkerAsync();
        }

        private void btout_Click(object sender, EventArgs e)
        {
            bgout.RunWorkerAsync();
        }

        private void bgmove_DoWork(object sender, DoWorkEventArgs e)
        {
            //string TrayNo = "1234567";
            int ActionType = 1;
            //int ActionType = 2;
            string fromx = "";
            string froml = "";
            string fromc = "";
            if (!string.IsNullOrEmpty(listboxstageloc.SelectedItem.ToString()))
            {
                fromx = listboxstageloc.SelectedItem.ToString().Substring(0, 1);
                froml = listboxstageloc.SelectedItem.ToString().Substring(2, 1);
                fromc = listboxstageloc.SelectedItem.ToString().Substring(4, 1);
            }
            else
            {
                MessageBox.Show("架台号不能为空");
            }

            string rec506 = ReadRightValue3("506", 1, "0001");
            if (rec506 != "0001")
            {
                //richtbxmsg.Text = "堆垛机非待机状态";
                richtbxmsg.AppendText("堆垛机上次非正常结束移动失败\r\n");
                //Log.WriteLog(LogFile.Trace,"堆垛机上次非正常结束，"+TrayNo+"搬送失败");
                Log.SystemError.Add("堆垛机上次非正常结束，移动失败");
                //Thread.Sleep(5000);
                return;
            }
            WriteRightValue3(30, 1, 6);

            WriteRightValue3(31, 1, ActionType);

            //From 
            WriteRightValue3(32, 1, int.Parse(fromx));
            WriteRightValue3(33, 1, int.Parse(froml));
            WriteRightValue3(34, 1, int.Parse(fromc));

            ////To的值
            //WriteRightValue3(35, 1, int.Parse(tox));
            //WriteRightValue3(36, 1, int.Parse(tol));
            //WriteRightValue3(37, 1, int.Parse(toc));

            //更新
            WriteRightValue3(10, 1, 1);

            WriteRightValue3(100, 1, 1);
            //等待W0510是否置1

            string rec510 = ReadRightValue3("510", 1, "0001");

            //if (rec510 != "0001")
            if (rec510 != "0001")
            {

                Log.SystemError.Add("PLCW510响应异常");
                richtbxmsg.AppendText("PLCW510响应异常\r\n");
                return;
            }
            WriteRightValue3(10, 1, 0);
            //richtbxmsg.AppendText("正在移动\r\n");
            tbxeqsts.Text = "移动中";
            tbxeqsts.BackColor = Color.SpringGreen;
            
            //while (ReadRightValue3("511", 1, "notnull") != "0001")
            while (ReadRightValue3("511", 1, "notnull") != "0001")
            {

                string str1 = ReadRightValue3("511", 1, "notnull");
                //Thread.Sleep(200);

            }
            tbxeqsts.Text = "已到达目标架台";
            tbxeqsts.BackColor = Color.SpringGreen;
            WriteRightValue3(11, 1, 2);
            if (ReadRightValue3("511", 1, "0000") == "0000")
            {
                WriteRightValue3(11, 1, 0);

            }
        }

        private void bgout_DoWork(object sender, DoWorkEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxtrayno.Text))
            {
                MessageBox.Show("托盘号不能为空");
                return;
            }
            string TrayNo = tbxtrayno.Text;
            int ActionType = 2;
            string fromx = "";
            string froml = "";
            string fromc = "";
            if (!string.IsNullOrEmpty(listboxstageloc.SelectedItem.ToString()))
            {
                fromx = listboxstageloc.SelectedItem.ToString().Substring(0, 1);
                froml = listboxstageloc.SelectedItem.ToString().Substring(2, 1);
                fromc = listboxstageloc.SelectedItem.ToString().Substring(4, 1);
            }
            else
            {
                MessageBox.Show("架台号不能为空");
            }

            string rec506 = ReadRightValue3("506", 1, "0001");
            if (rec506 != "0001")
            {
                //richtbxmsg.Text = "堆垛机非待机状态";
                richtbxmsg.AppendText("堆垛机上次非正常结束移动失败\r\n");
                //Log.WriteLog(LogFile.Trace,"堆垛机上次非正常结束，"+TrayNo+"搬送失败");
                Log.SystemError.Add("堆垛机上次非正常结束，移动失败");
                //Thread.Sleep(5000);
                return;
            }
            WriteRightValue3(30, 1, 6);

            WriteRightValue3(31, 1, ActionType);

            //From 
            WriteRightValue3(32, 1, int.Parse(fromx));
            WriteRightValue3(33, 1, int.Parse(froml));
            WriteRightValue3(34, 1, int.Parse(fromc));

            ////To的值
            //WriteRightValue3(35, 1, int.Parse(tox));
            //WriteRightValue3(36, 1, int.Parse(tol));
            //WriteRightValue3(37, 1, int.Parse(toc));

            //更新
            WriteRightValue3(10, 1, 1);

            WriteRightValue3(100, 1, 1);
            //等待W0510是否置1

            string rec510 = ReadRightValue3("510", 1, "0001");

            if (rec510 != "0001")
            {
                Log.SystemError.Add("PLCW510响应异常");
                richtbxmsg.AppendText("PLCW510响应异常\r\n");
                return;
            }
            WriteRightValue3(10, 1, 0);

            //string ldstatus = "";

            while (ReadRightValue3("511", 1, "notnull") != "0001")
            {
                string str1 = ReadRightValue3("511", 1, "notnull");
                //如果未出库的话才出库

                if (froml.PadLeft(4, '0') == "0009" && fromc.PadLeft(4, '0') == "0001")
                {

                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    // Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    //Thread.Sleep(200);
                    if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                    {
                        WriteRightValue3(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(91, 1, int.Parse(froml.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(92, 1, int.Parse(fromc.PadLeft(4, '0')));

                        Thread.Sleep(1000);
                        string rec152A = ReadRightValue3("152A", 1, "notnull");

                        string rec593 = ReadRightValue3("593", 1, "notnull");

                        if (rec152A == "0001" && rec593 == "0002")
                        {
                            //richtbxmsg.Text = "堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位";
                            //richtbxmsg.AppendText("堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位\r\n");
                            tbxeqsts.Text = "出库已就位";
                            tbxeqsts.BackColor = Color.SpringGreen;
                            WriteRightValue3(93, 1, 2);
                            //Thread.Sleep(200);
                            if (ReadRightValue3("593", 1, "0000") == "0000")
                            {
                                WriteRightValue3(93, 1, 0);
                                //richtbxmsg.Text = "堆垛机已完成出库正在搬送入库";
                                //richtbxmsg.AppendText("堆垛机已完成出库正在搬送入库\r\n");
                                tbxeqsts.Text = "出库已完成";
                                tbxeqsts.BackColor = Color.SpringGreen;

                            }
                            else
                            {
                                //richtbxmsg.Text = "PLC异常";
                                richtbxmsg.AppendText("PLC异常W0593未复位");
                                // Log.WriteLog(LogFile.SystemError, TrayNo + " W0593未复位\r\n");
                                Log.SystemError.Add(TrayNo + " W0593未复位");
                            }
                        }
                        else
                        {
                            Log.SystemError.Add("入口销钉下降失败,程序等待中");
                            richtbxmsg.AppendText("入口销钉下降失败,程序等待中");
                            Thread.Sleep(3000);
                        }

                        //
                    }


                }
                else if (froml.PadLeft(4, '0') == "0009" && fromc.PadLeft(4, '0') == "0002")
                {


                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    // Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    // Thread.Sleep(200);
                    if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                    {
                        WriteRightValue3(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(91, 1, int.Parse(froml.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(92, 1, int.Parse(fromc.PadLeft(4, '0')));
                        Thread.Sleep(1000);

                        string rec152C = ReadRightValue3("152C", 1, "notnull");
                        string rec593 = ReadRightValue3("593", 1, "notnull");
                        if (rec152C == "0001" && rec593 == "0002")
                        {
                            //richtbxmsg.Text = "堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位";
                            //richtbxmsg.AppendText("堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位\r\n");
                            tbxeqsts.Text = "出库已就位";
                            tbxeqsts.BackColor = Color.SpringGreen;
                            WriteRightValue3(93, 1, 2);
                            Thread.Sleep(200);
                            if (ReadRightValue3("593", 1, "0000") == "0000")
                            {
                                WriteRightValue3(93, 1, 0);
                                //richtbxmsg.Text = "堆垛机已完成出库正在搬送入库";
                                //richtbxmsg.AppendText("堆垛机已完成出库正在搬送入库\r\n");
                                tbxeqsts.Text = "已完成出库";
                                tbxeqsts.BackColor = Color.SpringGreen;

                            }
                            else
                            {
                                //richtbxmsg.Text = "PLC异常";
                                richtbxmsg.AppendText("PLC异常W0593未复位");
                                //Log.WriteLog(LogFile.SystemError, TrayNo + " W0593未复位\r\n");
                                Log.SystemError.Add(TrayNo + " W0593未复位");
                            }
                        }
                        else
                        {
                            Log.SystemError.Add("校验托盘销钉升起失败,程序等待中");
                            richtbxmsg.AppendText("校验托盘销钉升起失败,程序等待中");
                            Thread.Sleep(3000);
                        }

                        //
                    }



                }
                else if (froml.PadLeft(4, '0') != "0009")
                {
                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    //Thread.Sleep(200);

                    if (rec590 == fromx.PadLeft(4, '0') && rec591 == froml.PadLeft(4, '0') && rec592 == fromc.PadLeft(4, '0'))
                    {

                        //richtbxmsg.Text = "堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位";
                        //richtbxmsg.AppendText("堆垛机出库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位\r\n");
                        tbxeqsts.Text = "出库已就位";
                        tbxeqsts.BackColor = Color.SpringGreen;
                        WriteRightValue3(90, 1, int.Parse(fromx.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(91, 1, int.Parse(froml.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(92, 1, int.Parse(fromc.PadLeft(4, '0')));
                        // Thread.Sleep(200);
                        //
                        //Thread.Sleep(500);
                        string rec593 = ReadRightValue3("593", 1, "0002");
                        //Thread.Sleep(200);

                        if (rec593 == "0002")
                        {

                            //WUR
                            bool flag = kep.UpLoadingReservation(fromx.PadLeft(3, '0') + froml.PadLeft(3, '0') + fromc.PadLeft(3, '0'));
                            if (flag == false)
                            {
                                richtbxmsg.AppendText("装载托盘完成，ECS销钉下降失败\r\n");
                                Log.SystemError.Add(TrayNo + "ECS出库销钉下降失败\r\n");
                                while (flag == false)
                                {
                                    flag = kep.UpLoadingReservation(fromx.PadLeft(3, '0') + froml.PadLeft(3, '0') + fromc.PadLeft(3, '0'));
                                }
                            }
                            else
                            {

                                //lbwarn.Text = "销钉已下降请确认";
                                //lbwarn.BackColor = Color.Yellow;
                                Thread.Sleep(2000);
                                WriteRightValue3(93, 1, 2);
                                //Thread.Sleep(200);
                                if (ReadRightValue3("593", 1, "0000") == "0000")
                                {
                                    WriteRightValue3(93, 1, 0);
                                    //richtbxmsg.Text = "堆垛机已完成出库正在搬送入库";
                                    //richtbxmsg.AppendText("堆垛机已完成出库正在搬送入库\r\n");
                                    tbxeqsts.Text = "已完成出库";
                                    tbxeqsts.BackColor = Color.SpringGreen;

                                    //通知ECS Notify tray unloading completion   WTU

                                    bool flag1 = kep.TrayUnload(TrayNo.PadRight(20, ' '), fromx.PadLeft(3, '0') + froml.PadLeft(3, '0') + fromc.PadLeft(3, '0'));

                                    if (!flag1)
                                    {
                                        //Log.WriteLog(LogFile.SystemError, "WTU异常");
                                        Log.SystemError.Add("WTU异常");
                                        //richtbxmsg.AppendText = "WTU异常";
                                        richtbxmsg.AppendText("WTU异常\r\n");

                                        //continue;
                                    }
                                    else
                                    {
                                        //richtbxmsg.AppendText = "UpLoading托盘成功";
                                        //tbmsg.BackColor = Color.SpringGreen;

                                    }


                                }
                                else
                                {
                                    //Log.WriteLog(LogFile.SystemError, "W0593未复位");
                                    Log.SystemError.Add("W0593未复位");
                                    //richtbxmsg.Text = "PLC异常，请复位PLC";
                                    richtbxmsg.AppendText("PLC异常，请复位PLC\r\n");

                                }
                            }
                        }
                        else
                        {

                        }
                    }


                }

            }


            WriteRightValue3(11, 1, 1);
            if (ReadRightValue3("511", 1, "0000") == "0000")
            {
                WriteRightValue3(11, 1, 0);

            }
        }

        private void bgin_DoWork(object sender, DoWorkEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxtrayno.Text))
            {
                MessageBox.Show("托盘号不能为空");
                return;
            }
            string TrayNo = tbxtrayno.Text;
            int ActionType = 3;
            string tox = "";
            string tol = "";
            string toc = "";
            if (!string.IsNullOrEmpty(listboxstageloc.SelectedItem.ToString()))
            {
                tox = listboxstageloc.SelectedItem.ToString().Substring(0, 1);
                tol = listboxstageloc.SelectedItem.ToString().Substring(2, 1);
                toc = listboxstageloc.SelectedItem.ToString().Substring(4, 1);
            }
            else
            {
                MessageBox.Show("架台号不能为空");
            }

            string rec506 = ReadRightValue3("506", 1, "0001");
            if (rec506 != "0001")
            {
                //richtbxmsg.Text = "堆垛机非待机状态";
                richtbxmsg.AppendText("堆垛机上次非正常结束移动失败\r\n");
                //Log.WriteLog(LogFile.Trace,"堆垛机上次非正常结束，"+TrayNo+"搬送失败");
                Log.SystemError.Add("堆垛机上次非正常结束，移动失败");
                //Thread.Sleep(5000);
                return;
            }
            WriteRightValue3(30, 1, 6);

            WriteRightValue3(31, 1, ActionType);

            //From 
            WriteRightValue3(32, 1, int.Parse(tox));
            WriteRightValue3(33, 1, int.Parse(tol));
            WriteRightValue3(34, 1, int.Parse(toc));

            ////To的值
            //WriteRightValue3(35, 1, int.Parse(tox));
            //WriteRightValue3(36, 1, int.Parse(tol));
            //WriteRightValue3(37, 1, int.Parse(toc));

            //更新
            WriteRightValue3(10, 1, 1);

            WriteRightValue3(100, 1, 1);
            //等待W0510是否置1

            string rec510 = ReadRightValue3("510", 1, "0001");

            if (rec510 != "0001")
            {

                Log.SystemError.Add("PLCW510响应异常");

                richtbxmsg.AppendText("PLCW510响应异常\r\n");
                return;
            }
            WriteRightValue3(10, 1, 0);

            //string ldstatus = "";

            while (ReadRightValue3("511", 1, "notnull") != "0001")
            {

                if (tol.PadLeft(4, '0') == "0009" && toc.PadLeft(4, '0') == "0002")
                {

                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    //Thread.Sleep(200);

                    if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                    {
                        WriteRightValue3(90, 1, int.Parse(tox.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(91, 1, int.Parse(tol.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(92, 1, int.Parse(toc.PadLeft(4, '0')));
                        Thread.Sleep(500);

                        string rec152C = ReadRightValue3("152C", 1, "notnull");
                        string rec593 = ReadRightValue3("593", 1, "notnull");
                        //判断校验托盘存放位置的销钉是否已经上升。
                        if (rec152C == "0002" && rec593 == "0001")
                        {

                            tbxeqsts.Text = "入库已就位";
                            tbxeqsts.BackColor = Color.SpringGreen;
                            WriteRightValue3(93, 1, 1);
                            //Thread.Sleep(200);
                            if (ReadRightValue3("593", 1, "0000") == "0000")
                            {
                                WriteRightValue3(93, 1, 0);
                                //richtbxmsg.Text = "已完成入库";
                                //richtbxmsg.AppendText("已完成入库\r\n");
                                tbxeqsts.Text = "已完成入库";
                                tbxeqsts.BackColor = Color.SpringGreen;

                                Thread.Sleep(200);
                            }
                        }
                        else
                        {
                            Log.SystemError.Add("校验托盘位置的销钉升起失败,程序等待中");
                            richtbxmsg.AppendText("校验托盘位置的销钉升起失败,程序等待中");
                            Thread.Sleep(1000);
                        }
                    }


                }
                else if (tol.PadLeft(4, '0') == "0009" && toc.PadLeft(4, '0') == "0003")
                {


                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    //Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    //Thread.Sleep(200);
                    //Log.WriteLog(LogFile.Trace, "当前堆垛机位置是" + rec590 + "-" + rec591 + "-" + rec592);
                    if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                    {
                        WriteRightValue3(90, 1, int.Parse(tox.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(91, 1, int.Parse(tol.PadLeft(4, '0')));
                        //Thread.Sleep(200);
                        WriteRightValue3(92, 1, int.Parse(toc.PadLeft(4, '0')));


                        //richtbxmsg.Text = "入库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位";
                        //richtbxmsg.AppendText("入库" + rec590 + "-" + rec591 + "-" + rec592 + "已就位\r\n");
                        tbxeqsts.Text = "入库已就位";
                        tbxeqsts.BackColor = Color.SpringGreen;
                        Thread.Sleep(1000);
                        string rec152B = ReadRightValue3("152B", 1, "notnull");
                        string rec593 = ReadRightValue3("593", 1, "notnull");

                        //判断校验托盘存放位置的销钉是否已经上升。
                        if (rec152B == "0002" && rec593 == "0001")
                        {

                            WriteRightValue3(93, 1, 1);
                            Thread.Sleep(200);
                            if (ReadRightValue3("593", 1, "0000") == "0000")
                            {
                                WriteRightValue3(93, 1, 0);
                                //richtbxmsg.Text = "已完成入库";
                                //richtbxmsg.AppendText("已完成入库\r\n");
                                tbxeqsts.Text = "已完成入库";
                                tbxeqsts.BackColor = Color.SpringGreen;

                                Thread.Sleep(200);
                            }
                        }
                        else
                        {
                            Log.SystemError.Add("三层出库位置的销钉升起失败,程序等待中");
                            richtbxmsg.AppendText("三层出库位置的销钉升起失败,程序等待中");
                            Thread.Sleep(1000);
                        }
                    }


                }
                else if (tol.PadLeft(4, '0') != "0009")
                {
                    string rec590 = ReadRightValue3("590", 1, "notnull");
                    Thread.Sleep(200);
                    string rec591 = ReadRightValue3("591", 1, "notnull");
                    Thread.Sleep(200);
                    string rec592 = ReadRightValue3("592", 1, "notnull");
                    Thread.Sleep(200);
                    //Log.WriteLog(LogFile.Trace, "当前堆垛机位置是" + rec590 + "-" + rec591 + "-" + rec592);
                    //先验证入库位置是否正确
                    if (rec590 == tox.PadLeft(4, '0') && rec591 == tol.PadLeft(4, '0') && rec592 == toc.PadLeft(4, '0'))
                    {

                        tbxeqsts.Text = "入库已就位";

                        WriteRightValue3(90, 1, int.Parse(tox.PadLeft(4, '0')));

                        WriteRightValue3(91, 1, int.Parse(tol.PadLeft(4, '0')));

                        WriteRightValue3(92, 1, int.Parse(toc.PadLeft(4, '0')));

                        string rec593 = ReadRightValue3("593", 1, "0001");
                        //Thread.Sleep(200);
                        if (rec593 == "0001")
                        {
                            //现在是入库，请通知ECS销钉上升
                            //richtbxmsg.AppendText = "即将通知ECS销钉上升! !";
                            //tbmsg.BackColor = Color.SpringGreen;

                            string stageloc = tox.PadLeft(3, '0') + tol.PadLeft(3, '0') + toc.PadLeft(3, '0');
                            //bool flag = kep.TrayLoad(TrayNo, stageloc);
                            //bool ETLWrong = false;

                            //if (flag == false)
                            //{

                            //    while (!(GParam.RecListDic.ContainsKey("ETL") && GParam.RecStringDic.ContainsKey("ETL") && GParam.RecStringDic["ETL"].Length > 0 && GParam.RecStringDic["ETL"].Substring(17, 1) == "1"))
                            //    {


                            //        GParam.RecStringDic["ETL"] = "";
                            //        for (int i = 0; i < GParam.RecListDic["ETL"].Count; i++)
                            //        {
                            //            GParam.RecListDic["ETL"].RemoveAt(0);
                            //        }
                            //        richtbxmsg.AppendText("由于ECS销钉升起失败，等待人工处理中\r\n");
                            //        Log.WriteLog(LogFile.SystemError, TrayNo + "入库升降钉升起失败");

                            //        ETLWrong = true;

                            //        Thread.Sleep(3000);
                            //    }

                            //}

                            //if (ETLWrong == true)
                            //{
                            //    //销钉升起成功，即将回复WTL
                            //    GParam.RecStringDic["ETL"] = "";
                            //    string stageloc1 = GParam.RecListDic["ETL"][0].ToString().Substring(8, 9);
                            //    string trayno1 = "";
                            //    trayno1 = TrayNo;

                            //        bool flag1 = kep.TrayLoad(trayno1, stageloc);
                            //        if (flag1 == true)
                            //        {
                            //            richtbxmsg.AppendText("已解除故障，销钉已升起，即将开始充电\r\n");
                            //            Log.SystemError.Add("已解除故障，销钉已升起，即将开始充电");

                            //            //需要清空ETL的RecListDic

                            //        }
                            //        else
                            //        {
                            //            while (flag1 == false)
                            //            {
                            //                flag1 = kep.TrayLoad(trayno1, stageloc);
                            //            }

                            //        }
                            //    if (GParam.RecListDic["ETL"].Count > 0)
                            //    {
                            //        GParam.RecListDic["ETL"].RemoveAt(0);
                            //    }

                            //}
                            //else
                            //{

                            //}
                            //Thread.Sleep(5000);
                            WriteRightValue3(93, 1, 1);
                            Thread.Sleep(200);
                            if (ReadRightValue3("593", 1, "0000") == "0000")
                            {
                                WriteRightValue3(93, 1, 0);

                                //richtbxmsg.AppendText("已完成入库\r\n");
                                tbxeqsts.Text = "已完成入库";
                                tbxeqsts.BackColor = Color.SpringGreen;
                                //Thread.Sleep(200);


                            }


                        }




                    }
                }

            }


            WriteRightValue3(11, 1, 1);
            if (ReadRightValue3("511", 1, "0000") == "0000")
            {
                WriteRightValue3(11, 1, 0);

            }
        }
    }
}
